
<template>
    <BasicLayout>
        <template #wrapper>
            <el-card class="box-card">
                <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
                    <el-form-item label="订单号" prop="orderSn">
                        <el-input v-model="queryParams.orderSn" placeholder="请输入订单号" clearable size="small"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="手机号" prop="phone">
                        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small"
                            @keyup.enter.native="handleQuery" />
                    </el-form-item>
                    <el-form-item label="订单类型" prop="source">
                        <el-select v-model="queryParams.source" placeholder="雅眠帐目流水表订单类型" clearable size="small">
                            <el-option v-for="dict in sourceOptions" :key="dict.dictValue" :label="dict.dictLabel"
                                :value="dict.dictValue" />
                        </el-select>
                    </el-form-item>
                    <!--          <el-form-item label="交易号" prop="tradeSn">-->
                    <!--            <el-input-->
                    <!--              v-model="queryParams.tradeSn"-->
                    <!--              placeholder="请输入交易号"-->
                    <!--              clearable-->
                    <!--              size="small"-->
                    <!--              @keyup.enter.native="handleQuery"-->
                    <!--            />-->
                    <!--          </el-form-item>-->
                    <el-form-item label="流水类型" prop="type">
                        <el-select v-model="queryParams.type" placeholder="学校帐目流水表流水类型" clearable size="small">
                            <el-option v-for="dict in typeOptions" :key="dict.dictValue" :label="dict.dictLabel"
                                :value="dict.dictValue" />
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                    </el-form-item>
                </el-form>

                <el-table v-loading="loading" :data="schoolcashflowList" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" align="center" />
                    <el-table-column label="帐目标题" align="center" prop="title" :show-overflow-tooltip="true"
                        width="160" />
                    <el-table-column label="用户" align="center" prop="source" width="160">
                        <template slot-scope="scope">
                            <div class="row ali_cen">
                                <el-image style="width: 40px; height: 40px;border-radius:100%;margin-right:10px"
                                    :src="scope.row.userInfo.avatar"></el-image>
                                <div>{{scope.row.userInfo.nickname}}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="店铺" align="center" prop="source" width="160">
                        <template slot-scope="scope">
                            <div class="row ali_cen">
                                <el-image style="width: 40px; height: 40px;border-radius:100%;margin-right:10px"
                                    :src="scope.row.storeInfo.avatar"></el-image>
                                <div>{{scope.row.storeInfo.storeName}}</div>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true"
                        width="160" />
                    <el-table-column label="来源" align="center" prop="source" :formatter="sourceFormat" width="160">
                        <template slot-scope="scope">
                            {{ sourceFormat(scope.row) }}
                        </template>
                    </el-table-column>

                    <el-table-column label="流水类型" align="center" prop="type" :formatter="typeFormat" width="120">
                        <template slot-scope="scope">
                            {{ typeFormat(scope.row) }}
                        </template>
                    </el-table-column>
                    <el-table-column label="金额" align="center" prop="amount" :show-overflow-tooltip="true"
                        width="100" />

                    <el-table-column label="订单号" align="center" prop="orderSn" :show-overflow-tooltip="true"
                        width="200" />
                    <el-table-column label="管理员备注" align="center" prop="mark" :show-overflow-tooltip="true" />

                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
                        <template slot-scope="scope">
                            <el-button v-permisaction="['edu:cashflow:order']" size="mini" type="text"
                                icon="el-icon-edit" @click="handleUpdate(scope.row)">修改备注
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageIndex"
                    :limit.sync="queryParams.pageSize" @pagination="getList" />
                <!-- 添加或修改参数配置对话框 -->
                <el-dialog :title="title" :visible.sync="open" width="500px">
                    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

                        <el-form-item label="管理员备注" prop="mark">
                            <el-input v-model="form.mark" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"
                                placeholder="管理员备注" />
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="submitForm">确 定</el-button>
                        <el-button @click="cancel">取 消</el-button>
                    </div>
                </el-dialog>
            </el-card>
        </template>
    </BasicLayout>
</template>

<script>
import {
    addSchoolCashflow,
    delSchoolCashflow,
    getSchoolCashflow,
    listSchoolCashflow,
    updateSchoolCashflow
} from '@/api/edu/finance/cashflow'

export default {
    name: 'Config',
    data () {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            isEdit: false,
            // 类型数据字典
            typeOptions: [],
            schoolcashflowList: [],
            sourceOptions: [],
            // typeOptions: [],
            // 查询参数
            queryParams: {
                pageIndex: 1,
                pageSize: 10,
                cashflowId: undefined,
                orderSn: undefined,
                source: undefined,
                schoolId: undefined,
                tradeSn: undefined,
                type: undefined,
                userId: undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {}
        }
    },
    created () {
        this.getList()
        this.getDicts('edu_order_type').then((response) => {
            this.sourceOptions = response.data
        })

        this.getDicts('edu_cashflow_type').then((response) => {
            this.typeOptions = response.data
        })
    },
    methods: {
        /** 查询参数列表 */
        getList () {
            this.loading = true
            listSchoolCashflow(
                this.addDateRange(this.queryParams, this.dateRange)
            ).then((response) => {
                this.schoolcashflowList = response.data.list
                this.total = response.data.count
                this.loading = false
            })
        },
        // 取消按钮
        cancel () {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset () {
            this.form = {
                amount: undefined,
                cashflowId: undefined,
                mark: undefined,
                orderSn: undefined,
                source: undefined,
                schoolId: undefined,
                title: undefined,
                tradeSn: undefined,
                type: undefined,
                userId: undefined
            }
            this.resetForm('form')
        },
        sourceFormat (row) {
            return this.selectDictLabel(this.sourceOptions, row.source)
        },
        typeFormat (row) {
            return this.selectDictLabel(this.typeOptions, row.type)
        },

        /** 搜索按钮操作 */
        handleQuery () {
            this.queryParams.pageIndex = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery () {
            this.dateRange = []
            this.resetForm('queryForm')
            this.handleQuery()
        },
        /** 新增按钮操作 */
        handleAdd () {
            this.reset()
            this.open = true
            this.title = '添加学校帐目流水表'
            this.isEdit = false
        },
        // 多选框选中数据
        handleSelectionChange (selection) {
            this.ids = selection.map((item) => item.cashflowId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate (row) {
            this.reset()
            const cashflowId = row.cashflowId || this.ids
            getSchoolCashflow(cashflowId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改学校帐目流水表'
                this.isEdit = true
            })
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.cashflowId !== undefined) {
                        updateSchoolCashflow(this.form).then((response) => {
                            if (response.code === 200) {
                                this.msgSuccess('修改成功')
                                this.open = false
                                this.getList()
                            } else {
                                this.msgError(response.msg)
                            }
                        })
                    } else {
                        addSchoolCashflow(this.form).then((response) => {
                            if (response.code === 200) {
                                this.msgSuccess('新增成功')
                                this.open = false
                                this.getList()
                            } else {
                                this.msgError(response.msg)
                            }
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete (row) {
            const Ids = row.cashflowId || this.ids
            this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', '警告', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(function () {
                    return delSchoolCashflow(Ids)
                })
                .then(() => {
                    this.getList()
                    this.msgSuccess('删除成功')
                })
                .catch(function () { })
        }
    }
}
</script>
